package com.umpay.business.consume.impl;

import com.umpay.business.consume.ConsumeQueryBusiness;
import com.umpay.dict.DataDict;
import com.umpay.dict.XmlData;
import com.umpay.dict.XmlData4Mer;
import com.umpay.dict.retcode.MerAccessEC;
import com.umpay.dto.UMFResponse.Message;
import com.umpay.dto.UMFResponse.Message.Meta;
import com.umpay.dto.merAccess.res.consume.ConsumeQueryRes;
import com.umpay.dto.merBusi.res.consume.ConsumeQueryBusiRes;
import com.umpay.feign.client.ConsumeBusiServerFeignClient;
import com.umpay.util.PropertyUtil;
import com.umpay.util.RpidUtils;
import com.umpay.util.StringUtils;
import com.umpay.util.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: ConsumeQueryBusinessImpl  
 * @Description: 消费查询
 * @author zhangtao  
 * @date Feb 8, 2018
 */
@Component
public class ConsumeQueryBusinessImpl implements ConsumeQueryBusiness{
	
	private static final Logger log = LoggerFactory.getLogger(ConsumeQueryBusinessImpl.class);

	@Autowired
	private ConsumeBusiServerFeignClient consumeBusiServerFeignClient;
	@Autowired 
	private PropertyUtil propertyUtil;
	
	@Override
	public Message<ConsumeQueryRes> consumeQuery(Map<String, Object> reqMap) {
		log.info("【消费查询--步骤1】请求参数校验");
		log.info("【消费查询--步骤2】请求业务线查询消费订单");
		Message<ConsumeQueryBusiRes> merBusiRes = requstMerBusiQueryConsume(reqMap);
		log.info("【消费查询--步骤3】响应结果通知商户");
	    return assembleResponseData(merBusiRes);
	}
	private Message<ConsumeQueryBusiRes> requstMerBusiQueryConsume(Map<String, Object> reqMap){
		Map<String, Object> merBusiReqMap = new HashMap<String,Object>();
		merBusiReqMap.put(XmlData.RPID, RpidUtils.getRpid());
		merBusiReqMap.put(XmlData.MERID, StringUtils.trim(reqMap.get(XmlData4Mer.MER_ID)));
		merBusiReqMap.put(XmlData.BPRODUCTID,propertyUtil.getProperty("bsp.bproductid"));
		merBusiReqMap.put(XmlData.PPRODUCTID, "");
		merBusiReqMap.put(XmlData.REQDATE,TimeUtil.date8());
		merBusiReqMap.put(XmlData.REQTIME, TimeUtil.time6());
		merBusiReqMap.put(XmlData.VERSION, StringUtils.trim(reqMap.get(XmlData4Mer.VERSION)));
		merBusiReqMap.put(XmlData.ORDERID, StringUtils.trim(reqMap.get(XmlData4Mer.ORDER_ID)));
		merBusiReqMap.put(XmlData.ORDERDATE, StringUtils.trim(reqMap.get(XmlData4Mer.MER_DATE)));
		merBusiReqMap.put(XmlData.MERTRACE, StringUtils.trim(reqMap.get(XmlData4Mer.MER_TRACE)));
		log.info(String.format("消费查询：调用远程服务接口请求参数为:%s", merBusiReqMap));
		Message<ConsumeQueryBusiRes> merBusiRes = consumeBusiServerFeignClient.consumeQuery(merBusiReqMap);
		log.info(String.format("消费查询：调用远程服务接口请求参数为:%s", merBusiRes));
		return merBusiRes;
	}
	private Message<ConsumeQueryRes> assembleResponseData(Message<ConsumeQueryBusiRes> merBusiRes){
		Message<ConsumeQueryRes> resMessage = new Message<ConsumeQueryRes>();
		Meta resMeta = new Meta();
		if(merBusiRes != null) {
			Meta merBusiRet = merBusiRes.getMeta();
			String retCode = merBusiRet.getRet_code();
			String retMsg = merBusiRet.getRet_msg();
			resMeta.setRet_code(retCode);//返回码
			resMeta.setRet_msg(retMsg);//返回描述
			ConsumeQueryBusiRes resInfo = merBusiRes.getData();
			ConsumeQueryRes merAccessRes = new ConsumeQueryRes();
			if(resInfo != null) {
				merAccessRes.setMer_id(resInfo.getMerId());//商户号
				merAccessRes.setVersion(resInfo.getVersion());
				merAccessRes.setOrder_id(resInfo.getOrderId());
				merAccessRes.setMer_date(resInfo.getOrderDate());
				merAccessRes.setAmount(resInfo.getAmount());
				merAccessRes.setSettle_amt(resInfo.getSettleAmt());
				merAccessRes.setMer_check_date(resInfo.getMerCheckDate());
				merAccessRes.setTrade_no(resInfo.getTradeNo());
				merAccessRes.setTrade_state(resInfo.getTradeState());
				merAccessRes.setMer_trace(resInfo.getMerTrace());
			}
			merAccessRes.setVersion(DataDict.VERSION);//版本号
			resMessage.setData(merAccessRes);
		}else {
			resMeta.setRet_code(MerAccessEC.RESULT_UNKNOWN);
			resMeta.setRet_msg("交易结果不明");
		}
		resMessage.setMeta(resMeta);
		resMessage.setLinks(null);
		return resMessage;
	}
}
